package cn.kgc.dao.impl;

import cn.kgc.dao.BaseDao;
import cn.kgc.dao.StudentDao;
import cn.kgc.entity.Student;
import cn.kgc.util.Constands;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public List<Student> selectStudentPage(int begin) throws SQLException {
        List<Student> studentList = new ArrayList<>();
        String sql = "SELECT studentNo,studentName,sex,bornDate FROM student LIMIT ?,?";
        Object[] params = {begin, Constands.PAGE_SIZE};
        ResultSet resultSet = executeQuery(sql, params);
        while (resultSet.next()) {
            int studentNo = resultSet.getInt("studentNo");
            String studentName = resultSet.getString("studentName");
            String sex = resultSet.getString("sex");
            Timestamp bornDate = resultSet.getTimestamp("bornDate");
            Student student = new Student(studentNo, studentName, sex, bornDate);
            System.out.println("studentObj:" + student.toString());
            studentList.add(student);
        }
        return studentList;
    }

    @Override
    public int getCount() throws SQLException {
        int countNo = 0;
        String sql = "SELECT COUNT(1) AS countNo FROM student";
        Object[] params = {};
        ResultSet resultSet = executeQuery(sql, params);
        while (resultSet.next()) {
            countNo = resultSet.getInt("countNo");
        }
        return countNo;
    }

    @Override
    public int delStudentByStudentNo(int studentNo) {
        String sql = "DELETE FROM student WHERE studentNo = ?";
        Object[] params = {studentNo};
        return executeUpdate(sql, params);
    }

    @Override
    public int insertStudent(Student student) {
        String sql = "INSERT INTO student (studentNo,studentName,sex,bornDate) VALUES (?,?,?,?);";
        Object[] params = {student.getStudentNo(), student.getStudentName(), student.getSex(), student.getBornDate()};

        return executeUpdate(sql, params);
    }

    @Override
    public Student select(int studentNo) throws SQLException {
        String sql = "SELECT studentNo,studentname,sex,bornDate FROM student WHERE studentno = ?";
        Object[] params = {studentNo};
        ResultSet resultSet = executeQuery(sql, params);
        Student student = null;
        while (resultSet.next()) {
            int studentNo1 = resultSet.getInt("studentNo");
            String studentName = resultSet.getString("studentName");
            String sex = resultSet.getString("sex");
            Timestamp bornDate = resultSet.getTimestamp("bornDate");
            student = new Student(studentNo1, studentName, sex, bornDate);
            System.out.println("select method student:" + student.toString());
        }
        return student;
    }

    @Override
    public int update(Student student) {
        String sql = "update student set studentName = ? ,sex = ? ,bornDate = ? where studentNo = ?";
        Object[] params = {student.getStudentName(), student.getSex(), student.getBornDate(), student.getStudentNo()};

        return executeUpdate(sql, params);
    }
}
